package com.baiyun.bs.controller.admin;


import com.baiyun.bs.configuration.property.SystemConfig;
import com.baiyun.bs.viewmodel.admin.file.UeditorConfigVM;
import com.baiyun.bs.viewmodel.admin.file.UploadResultVM;
import com.baiyun.bs.base.BaseApiController;
import com.baiyun.bs.base.RestResponse;
import com.baiyun.bs.service.FileUpload;
import com.baiyun.bs.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Arrays;

/*
图片上传
 */
@RequestMapping("/api/admin/upload")
@RestController("AdminUploadController")
public class UploadController extends BaseApiController {

    private final FileUpload fileUpload;
    private final SystemConfig systemConfig;
    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);
    private static final String IMAGE_UPLOAD = "imgUpload";
    private static final String IMAGE_UPLOAD_FILE = "upFile";
    private static final String IMAGE_UPLOAD_FILE2 = "file";
    private final UserService userService;

    @Autowired
    public UploadController(FileUpload fileUpload, SystemConfig systemConfig, UserService userService) {
        this.fileUpload = fileUpload;
        this.systemConfig = systemConfig;
        this.userService = userService;
    }

    @ResponseBody
    @RequestMapping("/configAndUpload")
    public Object upload(HttpServletRequest request, HttpServletResponse response) {
        String action = request.getParameter("action");
        if (action.equals(IMAGE_UPLOAD)) {
            try {
                MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
                MultipartFile multipartFile = multipartHttpServletRequest.getFile(IMAGE_UPLOAD_FILE);
                long attachSize = multipartFile.getSize();
                String imgName = multipartFile.getOriginalFilename();
                String filePath;
                try (InputStream inputStream = multipartFile.getInputStream()) {
                    filePath = fileUpload.uploadFile(inputStream, attachSize, imgName);
                }
                String imageType = imgName.substring(imgName.lastIndexOf("."));
                UploadResultVM uploadResultVM = new UploadResultVM();
                uploadResultVM.setOriginal(imgName);
                uploadResultVM.setName(imgName);
                uploadResultVM.setUrl(filePath);
                uploadResultVM.setSize(multipartFile.getSize());
                uploadResultVM.setType(imageType);
                uploadResultVM.setState("SUCCESS");
                return uploadResultVM;
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        } else {
            UeditorConfigVM ueditorConfigVM = new UeditorConfigVM();
            ueditorConfigVM.setImageActionName(IMAGE_UPLOAD);
            ueditorConfigVM.setImageFieldName(IMAGE_UPLOAD_FILE);
            ueditorConfigVM.setImageMaxSize(2048000L);
            ueditorConfigVM.setImageAllowFiles(Arrays.asList(".png", ".jpg", ".jpeg", ".gif", ".bmp"));
            ueditorConfigVM.setImageCompressEnable(true);
            ueditorConfigVM.setImageCompressBorder(1600);
            ueditorConfigVM.setImageInsertAlign("none");
            ueditorConfigVM.setImageUrlPrefix("");
            ueditorConfigVM.setImagePathFormat("");
            return ueditorConfigVM;
        }
        return null;
    }


    @RequestMapping("/image")
    @ResponseBody
    public RestResponse questionUploadAndReadExcel(HttpServletRequest request) {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartHttpServletRequest.getFile("file");
        long attachSize = multipartFile.getSize();
        String imgName = multipartFile.getOriginalFilename();
        try (InputStream inputStream = multipartFile.getInputStream()) {
            String filePath = fileUpload.uploadFile(inputStream, attachSize, imgName);
            userService.changePicture(getCurrentUser(), filePath);
            return RestResponse.ok(filePath);
        } catch (IOException e) {
            return RestResponse.fail(2, e.getMessage());
        }
    }


    @ResponseBody
    @RequestMapping("/configAndUpload1")
    public Object upload1(HttpServletRequest request, HttpServletResponse response) {
        String Action = request.getHeader("Action");
        if ("imgUpload".equals(IMAGE_UPLOAD)) {
            try {
                MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
                MultipartFile multipartFile = multipartHttpServletRequest.getFile(IMAGE_UPLOAD_FILE2);
                long attachSize = multipartFile.getSize();
                String name = multipartFile.getName();
                String imgName = multipartFile.getOriginalFilename();
                String day = LocalDateTime.now().toString();
                String[] split = imgName.split("\\.");
                imgName = day + "." + split[1];
                String filePath;
                try (InputStream inputStream = multipartFile.getInputStream()) {
                    filePath = fileUpload.uploadFile(inputStream, attachSize, imgName);
                }
                String imageType = imgName.substring(imgName.lastIndexOf("."));
                UploadResultVM uploadResultVM = new UploadResultVM();
                uploadResultVM.setOriginal(imgName);
                uploadResultVM.setName(imgName);
                uploadResultVM.setUrl(filePath);
                uploadResultVM.setSize(multipartFile.getSize());
                uploadResultVM.setType(imageType);
                uploadResultVM.setState("SUCCESS");
                return uploadResultVM;
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        } else if ("videoUpload".equals(Action)) {
            try {
                MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
                MultipartFile multipartFile = multipartHttpServletRequest.getFile("videoFile"); // 调整参数以匹配您表单或客户端请求中的文件输入字段名
                long attachSize = multipartFile.getSize();
                String videoName = multipartFile.getOriginalFilename();
                String filePath;

                try (InputStream inputStream = multipartFile.getInputStream()) {
                    filePath = fileUpload.uploadFile(inputStream, attachSize, videoName);
                }

                String videoType = videoName.substring(videoName.lastIndexOf("."));
                // 假设您有一个类似于UploadResultVM的类用于视频上传结果
                UploadResultVM videoUploadResult = new UploadResultVM();
                videoUploadResult.setOriginal(videoName);
                videoUploadResult.setName(videoName);
                videoUploadResult.setUrl(filePath);
                videoUploadResult.setSize(multipartFile.getSize());
                videoUploadResult.setType(videoType);
                videoUploadResult.setState("SUCCESS");

                return videoUploadResult;
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }else if ("pptUpload".equals(Action)) {
            try {
                MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
                MultipartFile multipartFile = multipartHttpServletRequest.getFile("pptFile");
                long attachSize = multipartFile.getSize();
                String pptName = multipartFile.getOriginalFilename();
                String filePath;

                try (InputStream inputStream = multipartFile.getInputStream()) {
                    filePath = fileUpload.uploadFile(inputStream, attachSize, pptName);
                }

                String pptType = pptName.substring(pptName.lastIndexOf("."));
                UploadResultVM pptUploadResult = new UploadResultVM();
                pptUploadResult.setOriginal(pptName);
                pptUploadResult.setName(pptName);
                pptUploadResult.setUrl(filePath);
                pptUploadResult.setSize(multipartFile.getSize());
                pptUploadResult.setType(pptType);
                pptUploadResult.setState("SUCCESS");

                return pptUploadResult;
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        } else if ("wordUpload".equals(Action)) {
            try {
                MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
                MultipartFile multipartFile = multipartHttpServletRequest.getFile("wordFile");
                long attachSize = multipartFile.getSize();
                String wordName = multipartFile.getOriginalFilename();
                String filePath;

                try (InputStream inputStream = multipartFile.getInputStream()) {
                    filePath = fileUpload.uploadFile(inputStream, attachSize, wordName);
                }

                String wordType = wordName.substring(wordName.lastIndexOf("."));
                UploadResultVM wordUploadResult = new UploadResultVM();
                wordUploadResult.setOriginal(wordName);
                wordUploadResult.setName(wordName);
                wordUploadResult.setUrl(filePath);
                wordUploadResult.setSize(multipartFile.getSize());
                wordUploadResult.setType(wordType);
                wordUploadResult.setState("SUCCESS");

                return wordUploadResult;
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        } else if ("excelUpload".equals(Action)) {
            try {
                MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
                MultipartFile multipartFile = multipartHttpServletRequest.getFile("excelFile");
                long attachSize = multipartFile.getSize();
                String excelName = multipartFile.getOriginalFilename();
                String filePath;

                try (InputStream inputStream = multipartFile.getInputStream()) {
                    filePath = fileUpload.uploadFile(inputStream, attachSize, excelName);
                }

                String excelType = excelName.substring(excelName.lastIndexOf("."));
                UploadResultVM excelUploadResult = new UploadResultVM();
                excelUploadResult.setOriginal(excelName);
                excelUploadResult.setName(excelName);
                excelUploadResult.setUrl(filePath);
                excelUploadResult.setSize(multipartFile.getSize());
                excelUploadResult.setType(excelType);
                excelUploadResult.setState("SUCCESS");

                return excelUploadResult;
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
    } else {
                UeditorConfigVM ueditorConfigVM = new UeditorConfigVM();
                ueditorConfigVM.setImageActionName(IMAGE_UPLOAD);
                ueditorConfigVM.setImageFieldName(IMAGE_UPLOAD_FILE);
                ueditorConfigVM.setImageMaxSize(2048000L);
                ueditorConfigVM.setImageAllowFiles(Arrays.asList(".png", ".jpg", ".jpeg", ".gif", ".bmp"));
                ueditorConfigVM.setImageCompressEnable(true);
                ueditorConfigVM.setImageCompressBorder(1600);
                ueditorConfigVM.setImageInsertAlign("none");
                ueditorConfigVM.setImageUrlPrefix("");
                ueditorConfigVM.setImagePathFormat("");

                // 添加视频上传的配置
                ueditorConfigVM.setVideoActionName("videoUpload");
                ueditorConfigVM.setVideoFieldName("videoFile");
                ueditorConfigVM.setVideoMaxSize(104857600L);
                ueditorConfigVM.setVideoAllowFiles(Arrays.asList(".mp4", ".avi", ".mov", ".mkv"));

            // 添加PPT上传的配置
            ueditorConfigVM.setPptActionName("pptUpload");
            ueditorConfigVM.setPptFieldName("pptFile");
            ueditorConfigVM.setPptMaxSize(104857600L);
            ueditorConfigVM.setPptAllowFiles(Arrays.asList(".ppt", ".pptx"));

            // 添加Word上传的配置
            ueditorConfigVM.setWordActionName("wordUpload");
            ueditorConfigVM.setWordFieldName("wordFile");
            ueditorConfigVM.setWordMaxSize(104857600L);
            ueditorConfigVM.setWordAllowFiles(Arrays.asList(".doc", ".docx"));

            // 添加Excel上传的配置
            ueditorConfigVM.setExcelActionName("excelUpload");
            ueditorConfigVM.setExcelFieldName("excelFile");
            ueditorConfigVM.setExcelMaxSize(104857600L);
            ueditorConfigVM.setExcelAllowFiles(Arrays.asList(".xls", ".xlsx"));

            return ueditorConfigVM;
            }
            return null;
        }
}
